<template>
	<view class="container">
		<!-- <view class="left-bottom-sign"></view> -->
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack"></view>
		<view class="right-top-sign"></view>
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="wrapper">
			<view class="left-top-sign">LOGIN</view>
			<view class="welcome"> 账号注册！ </view>
			<view class="input-content">
				<!-- 手机号 -->
				<view class="input-item">
					<text class="tit">手机号码</text>
					<input type="number" :value="mobile" placeholder="请输入手机号码" maxlength="11" data-key="mobile" @input="inputChange" />
				</view>
				<!-- 验证码 -->
				<view class="input-item input-item-sms-code">
					<view class="input-wrapper">
						<view class="rf-input-wrapper">
							<view class="tit">验证码</view>
							<input type="number" v-model="code" placeholder="请输入验证码" maxlength="4" data-key="code" />
						</view>
						<button class="sms-code-btn" :loading="btnLoading" :disabled="smsCodeBtnDisabled" @confirm="toLogin" @tap.stop="getSmsCode">
						  <text v-if="!smsCodeBtnDisabled">获取验证码</text>
						  <text v-else class="sms-code-resend">{{ `重新发送 (${codeSeconds})` }}</text>
						</button>
					</view>
				</view>
				<!-- 密码 -->
				<view class="input-item" >
					<text class="tit">密码</text>
					<input type="mobile" value="" placeholder="请输入密码" placeholder-class="input-empty" maxlength="20" password data-key="password" @input="inputChange" @confirm="toLogin"/>
				</view>
				<!-- 再次输入密码 -->
				<view class="input-item" >
					<text class="tit">确认密码</text>
					<input type="mobile" value="" placeholder="请输入确认密码" placeholder-class="input-empty" maxlength="20" password data-key="repass" @input="inputChange" @confirm="toLogin" />
				</view>
				<!-- 昵称 -->
				<view class="input-item">
					<text class="tit">昵称</text>
					<input type="text" placeholder="请输入您的昵称" maxlength="12" data-key="nickname" nickname @input="inputChange" />
				</view>
				<!-- 邀请码 -->
				<view class="input-item">
					<text class="tit">邀请码</text>
					<input type="text" data-key="invit_code" placeholder="请输入您的邀请码" invit_code @input="inputChange" />
				</view>
			</view>
			<button class="confirm-btn" @click="toLogin" :disabled="logining">注册</button>
			<view class="register-section">已经注册了?<text @click="toRegist">马上登录</text></view>
		</view>
	</view>
</template>

<script>
	import {  mapMutations  } from 'vuex';
	
	export default{
		data(){
			return {
				mobile: '',
				password: '',
				repass:'',
				code:'',
				nickname:'',
				invit_code:'',
				logining: false,
				loginByPass: true,
				smsCodeBtnDisabled: false,
				btnLoading: false,
				s:60,//秒
				codeSeconds:"60s",
			}
		},
		onLoad(){
			
		},
		methods: {
			// 统一跳转路由
			// navTo(route) {
			// 	this.$mRouter.push({route});
			// },
			// 切换登录方式
			showLoginBySmsCode() {
				this.loginByPass = !this.loginByPass;
			},
			...mapMutations(['login']),
			inputChange(e){
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack(){
				uni.navigateBack();
			},
			toRegist(e){
				uni.navigateTo({
					url:'login'
				})
			},
			getSmsCode:function(){
				if(!this.$api.checkMobile(this.mobile)){
					this.$api.msg('手机号码格式不正确');
					this.logining = false;
					return false;
				}
				if(this.smsCodeBtnDisabled){return;}
				
				var mobile = this.mobile
				var that = this
				this.$api.postUrl(this.$api._api.ApiSmsCode,{mobile:mobile,type:1},function(res){
					let _data = {};
					if(typeof(res.data) == "object"){
						_data = res.data;
					}else{
						_data = JSON.parse(res.data);
					}
					if(_data.error===0){
						that.code = "1234";
						that.verifyShow = false;
						uni.showToast({
							title: "验证码发送成功"
						});
						that.smsCodeBtnDisabled = true;
						that.timer = setInterval(()=>{
							 that.timeCount()
						},1000)
					}else{
						uni.showToast({
							title: _data.message
						});
					}
				})
			},
			timeCount(){
				if(this.s == 0){
					this.smsCodeBtnDisabled = false;
					return clearTimeout();
				}else{
					--this.s;
					if(this.s < 0){
						this.s = 60;
					}
				}
				this.codeSeconds = String(this.s) + "s"
			},
			async toLogin(){
				this.logining = true;
				var mobile = this.mobile
				var password = this.password
				var repass = this.repass
				var code = this.code
				var nickname = this.nickname
				var invit_code = this.invit_code
				if(!this.$api.checkMobile(mobile)){
					this.$api.msg('手机号码格式不正确');
					this.logining = false;
					return false
				}
				if(!code){
					this.$api.msg('验证码不能为空')
					this.logining = false;
					return false
				}
				if(password !== repass){
					this.$api.msg('两次密码不一致')
					this.logining = false;
					return false
				}
				if(!nickname){
					this.$api.msg('请输入用户昵称')
					this.logining = false;
					return false
				}
				
				let clientid = "";
				// #ifdef APP-PLUS
				const clientInfo = plus.push.getClientInfo()
				clientid = clientInfo.clientid
				// #endif
		
				let that = this;
				let _param = {
					mobile:mobile,
					clientid:clientid,
					password:password,
					code:code,
					nickname:nickname,
					invit_code:invit_code
				}
				 this.$api.postUrl(this.$api._api.ApiUserRegister,_param,function(res){
					let _data = {};
					if(typeof(res.data) == "object"){
						_data = res.data;
					}else{
						_data = JSON.parse(res.data);
					}
					if(_data.status == 1){
						uni.navigateBack({
							delta:1
						})
					}else{
						that.$api.msg(_data.message)
						that.logining = false;
					}
				})
			}
		},
	}
</script>

<style lang='scss'>
	page{
		background: #fff;
	}
	.container{
		padding-top:100px;
		position:relative;
		width: 100vw;
		/* height: 100vh; */
		overflow: hidden;
		background: #fff;
	}
	.wrapper{
		position: relative;
		width: 100vw;
		z-index: 90;
		background: #fff;
		padding-bottom: 40px;
	}
	.back-btn{
		position:absolute;
		left: 40rpx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40rpx;
		font-size: 40rpx;
		color: $font-color-dark;
	}
	.left-top-sign{
		font-size: 120rpx;
		color: $page-color-base;
		position:relative;
		left: -16rpx;
	}
	.right-top-sign{
		position:absolute;
		top: 80rpx;
		right: -30rpx;
		z-index: 95;
		&:before, &:after{
			display:block;
			content:"";
			width: 400rpx;
			height: 80rpx;
			background: #b4f3e2;
		}
		&:before{
			transform: rotate(50deg);
			border-radius: 0 50px 0 0;
		}
		&:after{
			position: absolute;
			right: -198rpx;
			top: 0;
			transform: rotate(-50deg);
			border-radius: 50px 0 0 0;
			/* background: pink; */
		}
	}
	.left-bottom-sign{
		position:absolute;
		left: -270rpx;
		bottom: -320rpx;
		border: 100rpx solid #d0d1fd;
		border-radius: 50%;
		padding: 180rpx;
	}
	.welcome{
		position:relative;
		left: 50rpx;
		top: -90rpx;
		font-size: 46rpx;
		color: #555;
		text-shadow: 1px 0px 1px rgba(0,0,0,.3);
	}
	.input-content{
		padding: 0 60rpx;
	}
	.input-item{
		display:flex;
		flex-direction: column;
		align-items:flex-start;
		justify-content: center;
		padding: 0 30rpx;
		background:$page-color-light;
		height: 120rpx;
		border-radius: 4px;
		margin-bottom: 50rpx;
		&:last-child{
			margin-bottom: 0;
		}
		.tit{
			height: 50rpx;
			line-height: 56rpx;
			font-size: $font-sm+2rpx;
			color: $font-color-base;
		}
		input{
			height: 60rpx;
			font-size: $font-base + 2rpx;
			color: $font-color-dark;
			width: 100%;
		}	
	}

	.confirm-btn{
		width: 630rpx;
		height: 76rpx;
		line-height: 76rpx;
		border-radius: 50px;
		margin-top: 70rpx;
		background: $uni-color-primary;
		color: #fff;
		font-size: $font-lg;
		&:after{
			border-radius: 100px;
		}
	}
	.register-section{
		margin-top:40rpx;
		width: 100%;
		font-size:28rpx;
		color: $font-color-base;
		text-align: center;
		text{
			color: $font-color-spec;
			margin-left: 10rpx;
		}
	}
	.input-item-sms-code {
	  position: relative;
			width: 100%;
	  .sms-code-btn {
	    position: absolute;
	    color: #111;
	    right: 20rpx;
	    bottom: 20rpx;
	    font-size: 28rpx;
	  }
	
	  .sms-code-resend {
	    color: #999;
	  }
	
	  .sms-code-btn:after {
	    border: none;
	    background-color: transparent;
	  }
	}
</style>
